<?php
/**
 * xushuhui
 * 2017/8/15
 * 16:52
 */

namespace app\index\service;
use app\lib\exception\BankCardException;
use app\lib\exception\BaseException;
use app\lib\exception\IDCardException;
use think\Loader;
Loader::import('pingpp.identification', EXTEND_PATH);
/**
 * 银行卡验证类
 * Class Card
 * @package app\index\service
 * @author xushuhui
 * @version 1.0
 */

class Card
{
	// 身份证验证
	public static function idCard($data = [])
	{
		$json = \identification::instance()->identify('id_card',$data);
		$res = json_decode($json,true);
		if($res['result_code'] !== 0){
			self::recordError();
			throw new IDCardException([
				'msg'=>$res['message']
			]);
		}
		return true;

	}

	//记录身份验证错误次数
	public static function  recordError()
	{
		$ip = request()->ip();
		$error = session($ip);
		if($error >= 3){
			throw new IDCardException([
				'msg' => "错误达到".$error."次，请充值"
			]);

		}elseif($error < 3){
			return  session($ip,$error+1);
		}

	}

	//银行卡验证处理
	public static function bankCardPlace($cardData = [])
	{
		$json = \identification::instance()->identify('bank_card',$cardData);
		$res = json_decode($json,true);
		if($res['result_code'] !== 0){
			self::recordError();
			throw new BankCardException([
				'msg'=>$res['message']
			]);
		}
		return true;

	}
}